package com.okq.action;

import com.okq.action.base.BaseActionSupport;
import com.okq.entity.*;
import com.okq.service.IJobProductService;
import com.okq.service.IProductService;
import com.okq.util.BaseTool;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.PrintWriter;
import java.util.List;

/**
 * 描述 : 统计报表action
 * 作者 : zdl
 * 日期 : 2017/7/4 9:11
 */
public class ChartAction extends BaseActionSupport {
    private static final long serialVersionUID = 4141147137735415917L;
    /**
     * 产品接口
     */
    @Autowired
    private IProductService productServiceImpl;
    /**
     * 工单产品接口
     */
    @Autowired
    private IJobProductService jobProduceServiceImpl;

    /**
     * 产品
     */
    private Product product;
    /**
     * 工单产品
     */
    private JobProduct jobProduct;

    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(ChartAction.class);

    /**
     * 获取生产产品报表
     *
     * @return json
     */
    public String getProductChart() {
        PrintWriter out = null;
        try {
            out = this.getResponse().getWriter();
            User loginUser = (User) getSession().getAttribute("loginUser");
            if (loginUser != null) {
                String comCode = loginUser.getComCode();
                if (StringUtils.isNotBlank(comCode)) {
                    product.setComCode(comCode);
                }
                List<ProductChart> pcList = productServiceImpl.listProductByCondition(product);
                String data = BaseTool.toJson(pcList);
                out.print(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        } finally {
            if (out != null) {
                out.close();
            }
        }
        return null;
    }

    /**
     * 获取工单报表
     *
     * @return json
     */
    public String getJobChart() {
        PrintWriter out = null;
        try {
            out = this.getResponse().getWriter();
            User loginUser = (User) getSession().getAttribute("loginUser");
            if (loginUser != null) {
                String comCode = loginUser.getComCode();
                if (StringUtils.isNotBlank(comCode)) {
                    jobProduct.setComCode(comCode);
                }
                List<JobChart> jcList = jobProduceServiceImpl.listJobByCondition(jobProduct);
                String data = BaseTool.toJson(jcList);
                out.print(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        } finally {
            if (out != null) {
                out.close();
            }
        }
        return null;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public JobProduct getJobProduct() {
        return jobProduct;
    }

    public void setJobProduct(JobProduct jobProduct) {
        this.jobProduct = jobProduct;
    }
}
